package com.atguigu.singleton.type2;

public class SingletonTest02 {
    public static void main(String[] args) {
        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance1 == instance2);
        System.out.println(instance1.hashCode());
        System.out.println(instance2.hashCode());
    }
}


/**
 * 饿汉式-静态代码块
 */
class Singleton {

    /**
     * 2.本类中申请静态常量
     */
    private final static Singleton instance;

    /*
      3.使用静态代码块创建单例对象
     */
    static {
        instance = new Singleton();
    }

    /**
     * 1.私有化构造器,让外部不能new
     */
    private Singleton() {
    }

    /**
     * 4. 对外提供一个方法,返回单例对象
     *
     * @return 单例对象
     */
    public static Singleton getInstance() {
        return instance;
    }
}
